<%
    Users user = Trader.getUser(request.getParameter("uid"));
	wstrader.task.traffic.di.Instruction task = user.traffic.di.dIns;

	if (!user.Authenticated()) {
		response.sendRedirect("logon.jsp");
		return;
	}
	((Tasks)task.getOwner()).clearFocus();
	task.setFocus(true);	
	Trader.window_width = "90%";
	
	String action="0";
	if (request.getParameter("action_id")!=null) action = request.getParameter("action_id");
	System.out.println("actionId-----"+action);
%>

<%@include file="../header.jsp"%>

<%
	ResultSet rs=null;

	long focused_id = task.doTask();	
	Record rc = new Record(task.getDeliveryInstDetails());
	long new_id = task.getLastID("delivery_instruction") + 1;
	String RefNo="";
	
	String wgtid="0";
	if (request.getParameter("wgt_id") != null) wgtid = request.getParameter("wgt_id");
	Record delvyrc = task.getRecord("weight_note",Utils.strToLong(wgtid));
	long new_delvyid = task.getLastID("weight_note") + 1;
%>


<script language="javascript">
function listView()
{
	var form = document.formMain;
	form.action = "traffic.di.jsp";
	form.submit();
}

function doAddNewDi()
{
	var form = document.formMain;
	var value = "New DI";
	var value1 = "<%=task.getNew_DI()%>";
	if (addNewListItemById("delivery_instruction",value) < 0) return;
	form.new_rec.value="0";
	setValue("ref_number",value1);	

}

function doAddNewWN()
{
	var form = document.formMain;
	if (form.delivery_instruction.value > 0)
	{
		if (form.chk_tons.value > 1)
		{
			setValue("id",<%=new_delvyid%>);
			setValue("date","");
			setValue("ref_number_w","New Number");
			setValue("net_weight","0");
			setValue("no_of_bags_delvy","0");
			form.action_id.value="1";
			show("CardEntry");
		}
		else
		{
			alert("Cannot create weight notes for fully delivered DI");
		}	
	}
	else
	{
		alert("Please Select A Contract");
	}
}

function doUpdate()
{
	var form = document.formMain;
	doTask(1);
}

function doDI_Update()
{
	var form = document.formMain;
	form.action_id.value=0;
	doTask(3);
}

function doCalBags()
{
	var form = document.formMain;
	var tons = parseFloat(form.tons.value);
	var kgperbag = parseFloat(form.kg_per_bag.value);
	if (kgperbag > 0)
	{
		form.no_of_bags.value = formatNumber((tons*1000) / (kgperbag),0,false);
	}
	else
	{
		form.no_of_bags.value = 0;
	}
}

function dbClick(get_obj)
{
	var form = document.formMain;
	var f_id = get_obj.id;
	form.wgt_id.value=f_id;
	form.action_id.value="1";
	hide("CardEntry");	
	form.submit();
}

function del_DI()
{
	var form = document.formMain;
	alert(form.chk_tons.value);
	if (form.chk_tons.value <= 0)
	{
		alert("This DI has delivery registered. CANNOT BE DELETED.");
	}
	else
	{
		doTask(2);
	}
}

</script>

<link href="style.css" rel="stylesheet" type="text/css">					
<style type="text/css">
<!--
.style3 {font-weight: bold}
.style12 {
	font-size: 14px;
	font-weight: bold;
}
-->
</style>
<form method="POST" name="formMain" onSubmit="">				  
<table width="100%"  border="0" class="style2">
	<tr>
		<td colspan="3"><img src="images/delivery-instruction.jpg" width="300" height="30">	    </td>
	</tr>
	<tr class="style1" bgcolor="#EEEEEE">
	  <td width="17%" align="center"><label id="e11">DI Ref</label></td>
	  <td width="83%" colspan="2"><label id="e12">Detail
	      <input name="new_rec" type="hidden" id="new_rec" value="0">
	  </label></td>
  </tr>
	<tr>
	  <td valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><select name="delivery_instruction" size=15 class="style2" id="delivery_instruction" style="width:100%;" onChange="doPost()">
                    <%@include file="include/DeliveryInsRefNoList.jsp"%>

            <%//task.getResponse().setOptionX(task.getDI_List());%>
            <%//task.getResponse().setSelected("delivery_instruction", focused_id);%>
          </select></td>
        </tr>
        <tr>
          <td><img src="images/delete.gif" width="15" height="15" border="0" onClick="del_DI()"></td>
          </tr>
      </table></td>
	  <td  colspan="2" valign="top"><table width="100%"  border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td><table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style2" id="form_card_view">
            <tr>
              <td width="22%" align="right"><input type="hidden" name="action_id" id="action_id" value="<%=action%>" /></td>
              <td colspan="4">&nbsp;</td>
              <td width="12%">&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td width="22%" align="right">Ref Number &nbsp;</td>
              <td colspan="4"><strong><%=rc.getString("cnt_ref_number")%>
                <input name="contract_id" type="hidden" id="contract_id" value=<%=rc.getIntValue("contract_id")%>>
              </strong></td>
              <td colspan="3" rowspan="4"  align="left"><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                <tr>
                  <td align="right">&nbsp;</td>
                  <td><input type="hidden" name="grade_code" id="grade_code" class="style2" style="width:80px; text-align:" value="<%=task.getFieldValueForId("grade_master","grade_code",rc.getIntValue("grade_id"))%>" onChange="codeChanged(this)"></td>
                </tr>
                <tr>
                  <td width="24%" align="right">Origin &nbsp;</td>
                  <td width="76%"><select name="origin_id" id="origin_id" class="textnoedit" style="width:50%;" onChange="">
                    <option value="0"></option>
                    <%task.getResponse().setOption(task.getMasterList("origin_master"));%>
                    <%task.getResponse().setSelected("origin_id", rc.getIntValue("origin_id"));%>
                  </select></td>
                  </tr>
                <tr>
                  <td align="right">Quality &nbsp;</td>
                  <td align=""><select name="quality_id" id="quality_id" class="textnoedit" style="width:50%;" onChange="">
                    <option value="0"></option>
                    <%task.getResponse().setOption(task.getMasterList("quality_master"));%>
                    <%task.getResponse().setSelected("quality_id", rc.getIntValue("quality_id"));%>
                  </select></td>
                  </tr>
                <tr>
                  <td align="right">Grade &nbsp;</td>
                  <td align=""><select name="grade_id" id="grade_id" class="textnoedit" style="width:100%;" onChange="">
                    <option value="0"></option>
                    <%task.getResponse().setOption(task.getMasterList("grade_master"));%>
                    <%task.getResponse().setSelected("grade_id", rc.getIntValue("grade_id"));%>
                  </select></td>
                  </tr>
              </table></td>
              </tr>
			  <%
			  	
				String DiDt = Utils.dateToStr(Utils.today());
				String DiTm = Utils.timeToStr(Utils.now());
				String NewRec = request.getParameter("new_rec");
				String DelvyDt = "";
				String FrTm ="";
				String ToTm ="";
				String Rem ="";
				int WgtCnt=0;
				int QtyCnt=0;
				int Status=0;

				if (NewRec.compareTo("1")==0)
				{
					//RefNo="New Record";
				}
				else
				{
					RefNo=rc.getString("ref_number");
					DiDt = rc.getDate("date");
					DiTm = rc.getTime("time");
					DelvyDt = rc.getDate("delivery_date");
					FrTm = rc.getTime("from_time");
					ToTm = rc.getTime("to_time");		
					WgtCnt = rc.getIntValue("weight_controller_id");
					QtyCnt = rc.getIntValue("quality_controller_id");		
					Rem = rc.getString("remark");
					Status = rc.getIntValue("status");
				}
			  %>
            <tr>
              <td align="right"><label id="e21">Contract No &nbsp;</label></td>
              <td colspan="4"><strong><%=rc.getString("contract_no")%></strong></td>
              </tr>
            <tr>
              <td align="right"><label id="e22">DI Ref &nbsp;</label></td>
              <td colspan="4" style="font-weight:bold"><input name="ref_number" type="text" class="textnoedit" id="ref_number" value="<%=RefNo%>"></td>
              </tr>
            <tr>
              <td align="right"><label id="e24">DI Date &nbsp;</label></td>
              <td width="12%" style="font-weight:bold"><label id="di_date_"><%=DiDt%>
                  <input name="date" type="hidden" id="date" value =<%=DiDt%>>
              </label></td>
              <td colspan="3" style="font-weight:bold"><label id="di_time_"><%=DiTm%>
                  <input name="time" type="hidden" id="time" value =<%=DiTm%>>
              </label></td>
              </tr>
            <tr>
              <td align="right"><label id="e27">Supplier &nbsp;</label></td>
              <td colspan="4"><select name="supplier_id" id="supplier_id" class="textnoedit" style="width:100%;" onChange="">
                <option value="0"></option>
                <%task.getResponse().setOption(task.getCompanyList("supplier,buyer"));%>
                <%task.getResponse().setSelected("supplier_id", rc.getIntValue("supplier_buyer_id"));%>
              </select></td>
              <td><div align="right">ContractedTons &nbsp;</div></td>
              <td width="20%"><strong><%=rc.getString("cnttons")%></strong></td>
              <td width="19%">&nbsp;</td>
            </tr>
            <tr>
              <td align="right">Supplier Ref &nbsp;</td>
              <td colspan="4"><input type="text" name="supplier_ref" id="supplier_ref" class="style2" style="width:100%;" value="<%=rc.getString("supplier_buyer_ref")%>"></td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
<%
	//float tons          = ref.getFloatValue("tons");
	//float delivered_tons = ref.getFloatValue("delivered_tons");
	//float pending_tons = tons - delivered_tons;
%>			
            <tr>
              <td align="right"><label id="e29">Packing &nbsp;</label></td>
              <td colspan="4"><select name="packing_id" id="packing_id" class="style2" style="width:100%;" onChange="">
                <option value="0"></option>
                <%task.getResponse().setOption(task.getMasterList("packing_master"));%>
                <%task.getResponse().setSelected("packing_id", rc.getIntValue("packing_id"));%>
              </select></td>
              <td colspan="3" rowspan="4" align="left"><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                <tr>
                  <td width="25%" align="right">Delivered</td>
                  <%
				  	float delvbags = task.getSum("net_weight","inst_id","weight_note", rc.getLongValue("id"));
					float pendbags = task.getSum("tons","id","delivery_instruction", rc.getLongValue("id")) - delvbags;
					if (Status==1)
					{
						delvbags =rc.getFloatValue("tons");
						pendbags=0;
					}
					System.out.println("Delvy Bags:-" + delvbags);
					System.out.println("Pend Bags:-"+pendbags);
				  %>
                  <td width="21%" align="right"><strong><%=delvbags%></strong></td>
                  <td width="54%"> Mts </td>
                </tr>
                <tr>
                  <td align="right">Pending</td>
                  <td align="right"><strong><%=pendbags%></strong></td>
                  <td>Mts
                    <input type="hidden" name="chk_tons" id="chk_tons" value="<%=pendbags%>" /></td>
                </tr>
              </table></td>
              </tr>
            <tr>
              <td align="right"><label id="e211">DI Quantity &nbsp;</label></td>
              <td colspan="4"><input type="text" name="tons" id="tons" class="style2" style="width:80px; text-align:right" value="<%=rc.getFloat("tons",1)%>" onChange="doCalBags();">&nbsp;Mts</td>
              </tr>
            <tr>
              <td align="right">Kg per Bag &nbsp;</td>
              <td colspan="4"><input type="text" name="kg_per_bag" id="kg_per_bag" class="style2" style="width:80px; text-align:right" value="<%=rc.getIntValue("kg_per_bag")%>" onchange="doCalBags();"></td>
            </tr>
            <tr>
              <td align="right">No of Bags &nbsp;</td>
              <td colspan="4"><input type="text" name="wn_no_of_bags" id="wn_no_of_bags" class="style2" style="width:80px; text-align:right" value="<%=rc.getIntValue("no_of_bags")%>" onchange="doCalBags();"></td>
              </tr>
            <tr>
              <td align="right">Warehouse Delivery &nbsp;</td>
              <td colspan="4"><select name="warehouse_id" id="warehouse_id" class="style2" style="width:100%;" onChange="">
                <option value="0"></option>
                <%task.getResponse().setOption(task.getMasterList("warehouse_master"));%>
                <%task.getResponse().setSelected("warehouse_id", rc.getIntValue("warehouse_id"));%>
              </select></td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
              </tr>
            <tr>
              <td align="right">Delivery Date &nbsp;</td>
              <td colspan="4"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
                <tr>
                  <td width="72"><input type="text" name="delivery_date" id="delivery_date" class="style2" style="width:70px; text-align:center" value="<%=DelvyDt%>"></td>
                  <td><a onclick="popUpCalendar(this,'delivery_date','dd-mmm-yy');"><IMG width="20" height="20" border="0" src="js/calendar.gif" title="dd-MMM-yy (example: 01-Jan-04)"></a></td>
                </tr>
              </table></td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td align="right">Delivery Period &nbsp;</td>
              <td colspan="4"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
                <tr>
                  <td width="78"><em>From</em></td>
                  <td><em>To</em></td>
                </tr>
                <tr>
                  <td><input type="text" name="from_time" id="from_time" class="style2" style="width:70px; text-align:center" value="<%=FrTm%>"></td>
                  <td><input type="text" name="to_time" id="to_time" class="style2" style="width:70px; text-align:center" value="<%=ToTm%>"></td>
                </tr>
              </table></td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
			 <tr>
              <td align="right">Weight Controller &nbsp;</td>
              <td colspan="4"><select name="weight_controller_id" id="weight_controller_id" class="style2" style="width:100%;" onChange="">
                <option value="0"></option>
                <%task.getResponse().setOption(task.getCompanyList("controller"));%>
                <%task.getResponse().setSelected("weight_controller_id", WgtCnt);%>
              </select></td>
			  <td align="right">&nbsp;</td>
              <td colspan="2">&nbsp;</td>
			 </tr>
			 <tr>
			   <td align="right">Quality Controller &nbsp;</td>
			   <td colspan="4"><select name="quality_controller_id" id="quality_controller_id" class="style2" style="width:100%;" onChange="">
                 <option value="0"></option>
                 <%task.getResponse().setOption(task.getCompanyList("controller"));%>
                 <%task.getResponse().setSelected("quality_controller_id",QtyCnt);%>
               </select></td>
			   <td>&nbsp;</td>
			   <td colspan="2" align="center">&nbsp;</td>
			   </tr>
            <tr>
              <td align="right"><label id="e215">Remark &nbsp;</label></td>
              <td colspan="7"><textarea name="remark" rows="4" class="style2" id="remark" style="width:98%"><%=Rem%></textarea></td>
              </tr>
            <tr>
              <td align="right" height="24"><label id="e216">Completed &nbsp;</label></td>
              <td colspan="4"><input type="checkbox" name="status_" id="status_" onClick="cbxClick(this)" value="1"> <input type="hidden"    name="status"  id="status"  value="0"></td>
							
              <td align="right">&nbsp;</td>
              <td colspan="2" style="font-weight:bold">&nbsp;</td>
            </tr>
            <script language="javascript">
            	setCbx(getObj("status_"),     <%=Status%>);
			</script>
            <tr>
              <td align="right" height="24"><label id="e217">By Order Of &nbsp;</label></td>
              <td colspan="4"><strong><%//=rc.getString("user_name")%></strong>&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td colspan="2" style="font-weight:bold">&nbsp;</td>
            </tr>
            <tr>
              <td><div align="right"><img src="images/update.gif" width="15" height="15" border="0" onclick="doUpdate()" /></div></td>
              <td colspan="4">&nbsp;</td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td><div align="right"><span class="style12">Delivery Details</span></div></td>
              <td colspan="4">&nbsp;</td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="8" align="center"><table width="75%"  border="1" cellspacing="1" cellpadding="1">
                <tr align="center" bgcolor="#EEEEEE" class="style3">
                  <td width="7%" class="style2">Id</td>
                  <td width="15%" class="style2">Date</td>
                  <td width="15%" class="style2">Reference No.</td>
                  <td width="13%" class="style2">Tons</td>
                  <td width="18%" class="style2">No.Of.Bags</td>
                  </tr>
                <%
				ResultSet Rs = task.fetchDeliveryList();
				int dLoop=1;
				Rs.beforeFirst();
					while (Rs.next())
					{			
			%>
                <tr id="<%=Rs.getInt("id")%>" onclick="selectRow(this);" ondblclick="dbClick(this);">
                  <td align="left" class="style2">&nbsp;<%=Rs.getInt("id")%></td>
                  <td align="left" class="style2">&nbsp;<%=Utils.dateToStr(Rs.getDate("date"))%></td>
                  <td align="right" class="style2"><%=Rs.getString("ref_number")%>&nbsp;</td>
                  <td align="right" class="style2"><%=Utils.numberToStr(Rs.getFloat("net_weight"),3)%>&nbsp;</td>
                  <td align="right" class="style2"><%=Utils.numberToStr(Rs.getFloat("no_of_bags"),0)%>&nbsp;</td>
                  </tr>
                <%
						dLoop=dLoop+1;
					}
				if (dLoop==1)
				{
			%>
                <tr>
                  <td align="left">&nbsp;</td>
                  <td align="left">&nbsp;</td>
                  <td align="right">&nbsp;</td>
                  <td align="right">&nbsp;</td>
                  <td align="right">&nbsp;</td>
                  </tr>
                <%
				}
			%>
                <tr>
                  <td colspan="5" align="left"><img src="images/new.gif" width="15" height="15" onclick="doAddNewWN();" />&nbsp;<img src="images/delete.gif" width="15" height="15" onclick="" /></td>
                </tr>
              </table></td>
              </tr>
            
            <tr>
              <td>&nbsp;</td>
              <td colspan="4">&nbsp;</td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="8" align="center"><table id="CardEntry" width="80%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="33%" height="24" align="right" class="style2">Date</td>
                  <td width="1%">&nbsp;</td>
                  <td width="66%"><input name="delvy_date" type="text" id="delvy_date" value="<%=delvyrc.getDate("date")%>" />
                      <img src="js/calendar.gif" width="20" height="20" onclick="popUpCalendar(this,'delvy_date','dd-mmm-yy');" />
                      <input name="wgt_id" type="hidden" id="wgt_id" value="<%=wgtid%>" /></td>
                </tr>
                <tr>
                  <td height="24" align="right" class="style2">Reference No</td>
                  <td>&nbsp;</td>
                  <td><input name="ref_number_w" type="text" id="ref_number_w" value="<%=delvyrc.getString("ref_number")%>" /></td>
                </tr>
                <tr>
                  <td height="24" align="right" class="style2">Tons</td>
                  <td>&nbsp;</td>
                  <td><input name="net_weight" type="text" id="net_weight" value="<%=Utils.numberToStr(delvyrc.getFloatValue("net_weight"),3)%>"  /></td>
                </tr>
                <tr>
                  <td height="24" align="right" class="style2">No.Of. Bags</td>
                  <td>&nbsp;</td>
                  <td><input name="no_of_bags_delvy" type="text" id="no_of_bags_delvy" value="<%=Utils.numberToStr(delvyrc.getFloatValue("no_of_bags"),0)%>"  /></td>
                </tr>
                
                <tr>
                  <td>&nbsp;</td>
                  <td><img src="images/update.gif" width="15" height="15" onclick="doDI_Update();" /></td>
                  <td>&nbsp;</td>
                </tr>
              </table>
              	<script>
					var f = document.formMain;
					if (f.action_id.value=="0")
					{
						hide("CardEntry");
					}
					else
					{
						show("CardEntry");
					}
				</script>
              </td>
              </tr>
            
            <tr>
              <td>&nbsp;</td>
              <td colspan="4">&nbsp;</td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td colspan="4">&nbsp;</td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td colspan="4">&nbsp;</td>
              <td>&nbsp;</td>
              <td colspan="2">&nbsp;</td>
            </tr>
          </table></td>
        </tr>
      </table></td>
	</tr>
	<tr>
	  <td>&nbsp;</td>
		<td colspan="2"><table width="100%"  border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td width="13%">&nbsp;</td>
              <td align="right">
			  	<img src="images/report.jpg" width="55" height="18" border="0" onClick="print_DSI('di')">
			  	<img src="../shared/images/listview.jpg" width="55" height="18" border="0" onClick="listView();"></td>
          </tr>
          </table></td>
    </tr>
</table>
<%
	if (NewRec.compareTo("1")==0)
	{
%>
	<script>
		doAddNewDi();
	</script>
<%   
	}
%>	  
	  <input type="hidden" name="uid"  id="uid"  value="<%=user.getuid()%>">
	  <input type="hidden" name="task_id"  id="task_id"  value="0">
	  <input type="hidden" name="view" id="view" value="<%=task.getRequest().getIntValue("view")%>">
	  <input type="hidden" name="contracts"  id="contracts"  value="<%=rc.getInt("contract_id")%>">
	<input type="hidden" name="wr_show" id="wr_show" value="<%//=task.getRequest().getIntValue("wr_show")%>">
</form>

<%@include file="../footer.jsp"%>
